package cn.edu.wzut.controller;

import cn.edu.wzut.mbp.entity.SysRole;
import cn.edu.wzut.mbp.entity.SysUser;
import cn.edu.wzut.mbp.service.ISysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    HttpServletRequest req;

//    获取页面
    public Page getPage(){
        int current = ServletRequestUtils.getIntParameter(req,"current",1);
        int size = ServletRequestUtils.getIntParameter(req,"size",10);
        return new Page(current,size);
    }

    @GetMapping("/all")
    public JsonResult<Page<SysRole>>  PageAll() {
        Page<SysRole> sysRolePage= roleService.page(getPage());
        return new JsonResult<>(sysRolePage);

    }

    @GetMapping
    public JsonResult<List<SysRole>> all() {
        List<SysRole> sysRoleList=roleService.list();
        return new JsonResult<>(sysRoleList);
    }

    @GetMapping("/{id}")
    public JsonResult<SysRole> get(@PathVariable("id") BigDecimal id) {
        SysRole sysRole = roleService.getById(id);
        return new JsonResult<>(sysRole);
    }

//    模糊查询
//    @GetMapping("/{roleName}")
//    public JsonResult<List<SysRole>> get(@PathVariable("roleName") String roleName) {
//        List<SysRole> sysRoleList = roleService.getByRoleName(roleName);
//        return new JsonResult<>(sysRoleList);
//    }
//    模糊查询加分页
    @GetMapping("/search/{roleName}")
    public JsonResult<Page<SysRole>> get(@PathVariable("roleName") String roleName) {
        Page<SysRole> sysRolePage= roleService.page(getPage(),new QueryWrapper<SysRole>().like("ROLE_NAME","%"+roleName+"%"));
        return new JsonResult<>(sysRolePage);
    }
    @PostMapping
    public JsonResult<Object> save(@RequestBody SysRole sysRole) throws MyException {
        boolean b = roleService.saveOrUpdate(sysRole);
        if(b)
            return new JsonResult<>(0, "修改完成");
        else
            throw new MyException("修改失败");
    }
    @DeleteMapping("/{id}")
    public JsonResult<Object> deleteOne(@PathVariable("id") BigDecimal id) throws MyException {
        boolean b = roleService.removeById(id);
        if(b)
            return new JsonResult<>(0, "删除完成");
        else
            throw new MyException("删除失败");
    }
    @DeleteMapping
    public JsonResult<Object> deleteMany(@RequestBody BigDecimal[] ids) throws MyException {
        boolean b = roleService.removeByIds(Arrays.asList(ids));
        if(b)
            return new JsonResult<>(0, "删除完成");
        else
            throw new MyException("删除失败");
    }
}
